package com.moss.cloud.auth.biz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.moss.cloud.auth.biz.mapper.SysUserMapper;
import com.moss.cloud.auth.biz.service.ISysRoleService;
import com.moss.cloud.auth.biz.service.ISysUserRoleService;
import com.moss.cloud.auth.biz.service.ISysUserService;
import com.moss.cloud.common.base.model.po.SysRole;
import com.moss.cloud.common.base.model.po.SysUser;
import com.moss.cloud.common.base.model.po.SysUserRole;
import com.moss.cloud.common.core.constant.Constant;
import com.moss.cloud.common.core.enums.state.Deleted;
import com.moss.cloud.common.core.model.vo.UserInfo;
import com.moss.cloud.common.core.utils.BooleanHandel;
import com.moss.cloud.common.security.component.RedisTemplateOps;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 用户服务层实现
 *
 * @author 瑾年
 * @data 2023年3月6日
 */
@Slf4j
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements ISysUserService {
    @Resource
    ISysUserRoleService sysUserRoleService;
    @Resource
    ISysRoleService sysRoleService;

    @Resource
    RedisTemplateOps redisTemplateOps;

    /**
     * 根据手机号，账号，邮箱查询用户信息
     *
     * @param userName
     * @return
     */
    @Override
    public UserInfo userInfo(String userName) {
        UserInfo userInfo = new UserInfo();
        SysUser sysUser = this.getUserByUsername(userName);
        BooleanHandel.isTrue(Objects.nonNull(sysUser)).trueHandle(() -> {
            List<SysUserRole> userRoles = sysUserRoleService.findRoleIdByUserId(sysUser.getId());
            BeanUtils.copyProperties(sysUser, userInfo);
            List<Long> roleIds = CollectionUtils.isNotEmpty(userRoles) ?
                    userRoles.stream().map(item -> item.getRoleId())
                            .collect(Collectors.toList()) : Collections.emptyList();
            userInfo.setRoleIds(roleIds);
            BooleanHandel.isTrue(CollectionUtils.isNotEmpty(roleIds)).trueHandle(() -> {
                List<SysRole> sysRoles = sysRoleService.list(new QueryWrapper<SysRole>().lambda().in(SysRole::getId, roleIds));
                userInfo.setRoleCodes(CollectionUtils.isNotEmpty(sysRoles) ? sysRoles.stream()
                        .map(item -> item.getCode()).collect(Collectors.toList()) : Collections.emptyList());
            });
        });
        return userInfo;
    }

    /**
     * 缓存当前登录用户信息
     *
     * @param userName
     */
    @Override
    public void cacheUserInfo(String userName) {
        BooleanHandel.isTrue(!Constant.DEF_CLIENT.equals(userName)).trueHandle(() -> {
            UserInfo userInfo = this.userInfo(userName);
            userInfo.setPassword("");
            redisTemplateOps.set(Constant.LOGIN_USER + userName, userInfo, Constant.LOGIN_USER_TIME);
        });
    }

    /**
     * 根据授权的账号获取用户信息
     *
     * @param userName
     * @return
     */
    public SysUser getUserByUsername(String userName){
        return this.getOne(new QueryWrapper<SysUser>().lambda().eq(SysUser::getUsername, userName)
                .or().eq(SysUser::getMobile, userName)
                .or().eq(SysUser::getEmail, userName)
                .or().eq(SysUser::getOpenId, userName).notIn(SysUser::getDeleted, Deleted.DELETE.code()));
    }

}
